var app = getApp()
var charts = require('../../../../../common/view/mars/charts');
const UmfToast = require('../../../../../utils/UmfToast.js');
const network_utils = require('../../../../../http/NetworkUtils.js');
const constants = require('../../../../../common/Constants.js');
const umfUtils = require('../../../../../utils/UmfUtils.js');
import PageRouter from '../../../../../utils/PageRouter';

var app = getApp();
let pageOptions = {};
let pageLength = 4;
let page = null;

Page({
    data: {
        //月账单
        dataArray: [],
        hasMore: false,
        graphArray: [],
    },

    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        page = 0;
        let res = app.globalData.systemInfo;
        let params = JSON.parse(options.params);
        this.setData({
            deviceW: res.windowWidth,
            deviceH: res.deviceHeight,

            fromPage: params.fromPage
        })
        wx.setNavigationBarTitle({
            title: (options.title + '月账单' || "")
        });
        pageOptions = options;
        this._netWorkRequest();
    },

    loadMore: function () {
        this._netWorkRequest();

    },

    getCheckEmail: function () {
        //获取对账邮件
        let url = './checkForEmail/checkForEmail?title=' + pageOptions.title + '&storeId=' + pageOptions.storeId
        PageRouter.wxNavigateTo(this, url, this.data.fromPage === 'searchStores'|| this.data.fromPage === "clerkTransOverview");
    },

    _netWorkRequest: function () {
        let that = this;
        let params = {
            storeId: pageOptions.storeId,
            pageNo: page + 1 + "",
            pageSize: pageLength + "",
        };

        network_utils.buildRequest(constants.Interfaces.FUNC_Merchant_MonthAccount, params, {
            doHttpSucess: function (data) {
                if (constants.RetCodes.SUCCESS === data.retCode) {
                    page++;
                    that._dataFormate(data);
                    return true;
                }
                return false;
            }
        }).send();
    },

    //格式化数据
    _dataFormate: function (data) {

        let dataList = [];
        Object.assign(dataList, data["tradeList"]);
        let tradeListArray = this.data.dataArray;
        for (let i = 0; i < dataList.length; i++) {

            dataList[i]["amount"] = umfUtils.fentoyuan(dataList[i]["amount"]);

            dataList[i]["date2"] = getYearAndMonth(dataList[i]["date"]);
            tradeListArray.push(dataList[i]);
        }
        let hasMore = dataList.length === parseInt(pageLength);
        !hasMore && UmfToast.showToast("已经加载完所有数据");
        this.setData({
            dataArray: tradeListArray,
            hasMore: hasMore,
        });
        if (page == 1) {
            this._initGraph(dataList);
        }
    },

    //初始化图表
    _initGraph: function (data) {
        let amountArray = [];
        let month = [];
        for (let i = data.length - 1; i >= 0; i--) {
            month.push(umfUtils.getMonthWithDate(data[i]["date"]));
            amountArray.push(parseFloat(data[i]["amount"]));
        }
        this.setData({
            graphArray: month
        });

        let params = {}
        params.width = this.data.deviceW;
        params.height = 150;
        params.canvas_id = 'lineGraph';
        params.ytitle = '';
        params.xcate = month;
        params.yAxisSplit = 5;
        params.min = 0;
        params.data = [{
            name: '月份',
            data: amountArray,
            format: function (val) {
                return val.toFixed(2);
            }
        }]
        charts.shapeLine(params);
    },

})

function getYearAndMonth(dateStr) {
    let formatDate = new Date(dateStr);
    let month = formatDate.getMonth() + 1;
    return formatDate.getFullYear() + '年' + month + '月';
}